<template>
    <el-container>
        <el-header></el-header>
        <el-main>
            <el-row>
                <el-col :span="5"></el-col>
                <el-col :span="15">
                    <router-link to="/personal" style="text-decoration-line: none;">
                        <h5 style="margin-top: 0px">&lt; 返回</h5>
                    </router-link>
                    <div style="background-color: lightskyblue;height: 25px;margin-top: -10px">
                        <h5 style="padding-top: 2px">我的收货地址</h5>
                    </div>
                    <h5 style="color: red;margin-top: 2px">新增地址</h5>
                    <div style="width: 400px">
                        <el-form :model="form" label-width="100px" style="margin-left: -30px">
                            <el-form-item label="姓名">
                                <el-input v-model="form.name"/>
                            </el-form-item>
                            <el-form-item label="手机号">
                                <el-input v-model="form.telephone"/>
                            </el-form-item>
                            <el-form-item label="地址">
                                <el-input v-model="form.address"/>
                            </el-form-item>
                            <el-form-item label="默认">
                                <el-switch v-model="value"
                                           active-value="1"
                                           inactive-value="0"/>
                            </el-form-item>
                            <el-form-item>
                                <el-button @click="OnSumbit">确认</el-button>
                            </el-form-item>

                        </el-form>
                    </div>
                    <el-table :data="tableData" style="width: 100%;border: 3px solid gainsboro;">
                        <el-table-column type="index" label="序号" width="100"/>
                        <el-table-column prop="name" label="姓名" width="120"/>
                        <el-table-column prop="telephone" label="手机号" width="120"/>
                        <el-table-column prop="address" label="地址" width="180"/>
                        <el-table-column label="操作">
                            <template #default="scope">
                                <el-button size="small" @click="handleEdit(scope.row.id)"
                                type="success">修改
                                </el-button
                                >
                                <el-button
                                        size="small"
                                        type="danger"
                                        @click="handleDelete(scope.row.id)"
                                >删除
                                </el-button
                                >
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template #default="scope">
                                <el-switch v-model="scope.row.sta"
                                           :active-value="1"
                                           :inactive-value="0"
                                           @click = "editSta(scope.row)"
                                />
                            </template>
                        </el-table-column>
                    </el-table>

                    <el-dialog v-model="dialogFormVisible" title="编辑地址信息">
                        <el-form :model="editForm">
                            <el-form-item label="姓名: " :label-width="60">
                                <el-input v-model="editForm.name" autocomplete="off" style="width: 260px"/>
                            </el-form-item>
                            <el-form-item label="手机号: " :label-width="60">
                                <el-input v-model="editForm.telephone" autocomplete="off" style="width: 260px"/>
                            </el-form-item>
                            <el-form-item label="地址: " :label-width="60">
                                <el-input v-model="editForm.address" autocomplete="off" style="width: 260px"/>
                            </el-form-item>
                            <el-form-item :label-width="60">
                                <el-button type="success" @click="editSubbit">修改</el-button>
                            </el-form-item>
                        </el-form>
                    </el-dialog>
                </el-col>
            </el-row>
        </el-main>
    </el-container>
</template>
<script lang="ts" setup>
import {
    Document,
    Menu as IconMenu,
    Location,
    Setting, User,
} from '@element-plus/icons-vue'

import {onMounted, ref, reactive, toRaw, computed} from 'vue'
import {ElMessageBox, ElTable} from 'element-plus'
import axios from 'axios'
import router from '../router/index.js'
import {ElMessage} from "element-plus";


onMounted(() => {
    address()
})

const tableData = ref([])
const address = () => {
    axios.get('http://127.0.0.1:8080/address/queryByUid').then(res => {
        tableData.value = res.data

    }).catch(error => {
        console.log(error)
    })
}
console.log(tableData)
//删除
const handleDelete = (index: number) => {
    ElMessageBox.confirm(
        '是否确认删除这条信息',
        '操作',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            axios.delete('http://127.0.0.1:8080/address/remove/' + index).then(res => {
                if (res.data) {
                    address()
                }
            }).catch(error => {
                console.log(error)
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })

}
// 新增
const value = ref(0)
const form = reactive({
    name: '',
    address: '',
    telephone: '',
    sta: ''
})

const OnSumbit = () => {
    form.sta = value.value
    axios.post('http://127.0.0.1:8080/address/create', form).then(res => {
        if (res.data) {
            address()
            form.address = ''
            form.name = ''
            form.telephone = ''
            value.value = 0
            ElMessage({
                message: '添加地址成功',
                type: 'success',
            })
        }
    }).catch(error => {
        console.log(error)
    })
}

//编辑
const dialogFormVisible = ref(false)
const editForm = reactive({
    id:'',
    name: '',
    telephone: '',
    address: ''
})
const handleEdit = (index: number) => {
    dialogFormVisible.value = true;
    axios.get('http://127.0.0.1:8080/address/queryId/' + index).then(res => {
        if(res.data){
            editForm.id = res.data.id
            editForm.name = res.data.name;
            editForm.telephone = res.data.telephone;
            editForm.address = res.data.address;
        }
    }).catch(error => {
        console.log(error)
    })

}
const editSubbit = ()=>{
    axios.put('http://127.0.0.1:8080/address/modifyAdr',editForm).then(res=>{
        if (res.data){
            address()
            ElMessage({
                message: '编辑成功',
                type: 'success',
            })
            dialogFormVisible.value = false;
        }
    })
}
//改变默认地址
const editSta = (address1)=>{

    axios.put('http://127.0.0.1:8080/address/modifySta',address1).then(res=>{
        if (res.data){
           setTimeout(()=>{
               address()
           },1000)
        }
    }).catch(error=>{
        console.log(error)
    })
}
</script>
<style scoped>
.el-header {
    height: 60px;
    background-color: #545c64;
    position: relative;
}
</style>